package tau_sadna.tau_map;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExtraInfo extends RelativeLayout{

	private MainActivity mainActivity;

	public ExtraInfo(MainActivity main, String text, String name, String pic) {
		super(main);
		
		mainActivity = main;
		
		Log.i("ExtraInfo", "recieved pic: "+pic);
		setBackgroundColor(Color.WHITE);
		
		TextView nameTextView = new TextView(main);
		nameTextView.setId(830);
		nameTextView.setText(name);
		nameTextView.setGravity(Gravity.CENTER);
		nameTextView.setTextSize(20);
		nameTextView.setTextColor(Color.BLUE);
		
		TextView textView = new TextView(main);
		textView.setId(831);
		textView.setText(text);
		textView.setGravity(Gravity.CENTER);
		textView.setTextSize(20);
		
		ImageView button = new ImageView(main);
		button.setImageDrawable(main.getResources().getDrawable(R.drawable.red_x));
		button.setId(829);
		OnClickListener listener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				mainActivity.closeCurrentWindow();
			}
			
		};
		button.setOnClickListener(listener);
		
		ImageView ex_pic = new ImageView(main);
		if (pic!=null) {
			Drawable drawable = null;
			try {
				drawable = main.getResources().getDrawable((Integer) R.drawable.class.getField(pic).get(R.drawable.class));
			} catch (Exception e) {
				e.printStackTrace();
			}
			ex_pic.setImageDrawable(drawable);
		}
		ex_pic.setId(850);
		
		RelativeLayout.LayoutParams buttonParam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
		buttonParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		if (new MLString("a", "b").getName().equals("a")) {
			buttonParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		} else {
			buttonParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		}
		
		button.setLayoutParams(buttonParam);
		
		if (pic!=null) {
			RelativeLayout.LayoutParams picParam = new RelativeLayout.LayoutParams(
	                RelativeLayout.LayoutParams.MATCH_PARENT,
	                RelativeLayout.LayoutParams.WRAP_CONTENT);
	
			picParam.addRule(RelativeLayout.BELOW, button.getId());
			picParam.addRule(RelativeLayout.BELOW, nameTextView.getId());
			ex_pic.setLayoutParams(picParam);
		}
		
		RelativeLayout.LayoutParams nameParam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
		if (new MLString("a", "b").getName().equals("b")) {
			nameParam.addRule(RelativeLayout.LEFT_OF, button.getId());
		} else {
			nameParam.addRule(RelativeLayout.RIGHT_OF, button.getId());
		}
		nameParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		
		RelativeLayout.LayoutParams textParam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
		textParam.addRule(RelativeLayout.BELOW, button.getId());
		if (pic == null) {
			textParam.addRule(RelativeLayout.BELOW, nameTextView.getId());
		} else {
			textParam.addRule(RelativeLayout.BELOW, ex_pic.getId());
		}
		
		
		nameTextView.setLayoutParams(nameParam);
		textView.setLayoutParams(textParam);
		
		addView(button);
		if (pic!=null) {
			addView(ex_pic);
		}
		addView(nameTextView);
		addView(textView);
	}

}
